<?php
class TinraosController extends AppController {

	var $name = 'Tinraos';
	var $paginate = array(
		'limit' => 10,
		'order' => array(
			'Tinrao.vitrixh' => 'asc'
		)
    );
	function index() {
		$this->Tinrao->recursive = 0;
		$this->set('tinraos', $this->paginate());
	}
	function admin_index() {
		$this->layout = 'admin';
		$this->Tinrao->recursive = 0;
		$this->set('tinraos', $this->paginate());
	}
	function tinraosleft(){
		$data = $this->paginate('Tinrao');
		if (isset($this->params['requested'])) {
			return $data;
        }
		$this->set('data', $data);
	}
	function tinraosright(){
		$this->paginate = array(
								'limit' => 25,
								'order' => array(
									'Tinrao.ngaydang' => 'desc'
								)
							);
		$data = $this->paginate('Tinrao');
		if (isset($this->params['requested'])) {
			return $data;
        }
		$this->set('data', $data);
	}
	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid tinrao', true));
			$this->redirect(array('action' => 'index'));
		}
		$this->set('tinrao', $this->Tinrao->read(null, $id));
	}

	/* function add() {
		$this->layout = 'admin';
		if (!empty($this->data)) {
			$this->Tinrao->create();
			if ($this->Tinrao->save($this->data)) {
				$this->Session->setFlash(__('The tinrao has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tinrao could not be saved. Please, try again.', true));
			}
		}
	} */
	function add() {
		$this->layout = 'duanview';
		$this->data['Tinrao']['tieude'] = $_POST['tieude'];
		$this->data['Tinrao']['duan_id'] = $_POST['idduan'];
		$this->data['Tinrao']['noidung'] = $_POST['noidung'];
		$this->data['Tinrao']['tenlienhe'] = $_POST['tenlienhe'];
		$this->data['Tinrao']['vitrixh'] = 9;
		$this->data['Tinrao']['sdtlienhe'] = $_POST['sdtlienhe'];
		$this->data['Tinrao']['email'] = $_POST['email'];
		$this->data['Tinrao']['ngaydang'] = date('Y-m-d');
		
		$i = $this->data['Tinrao']['vitrixh'];
		while(1){
			$tinrao = $this->Tinrao->find('first', array('conditions' => array('Tinrao.vitrixh' => $i)));
			if($tinrao){
				$i++;
			}else{
				break;
			}
		}
		$query = 'UPDATE tinraos SET vitrixh='.$i.' WHERE vitrixh = '.$this->data['Tinrao']['vitrixh'];
		$this->Tinrao->query($query);
		print_r($this->data['Tinrao']['vitrixh']);
		if ($this->Tinrao->save($this->data)) {
			$this->Session->setFlash(__('The tinrao has been saved', true));
			$this->redirect(array('controller'=>'duans', 'action' => 'view/'.$_POST['idduan']));
		} else {
			$this->Session->setFlash(__('The tinrao could not be saved. Please, try again.', true));
		} 
		
	}
	function change_vitrixh(){
		$idtinrao = $_POST['idtinrao'];
		$vitriedit = $_POST['vitrixh'];
		
		$i = $vitriedit;
		while(1){
			$tinrao = $this->Tinrao->find('first', array('conditions' => array('Tinrao.vitrixh' => $i)));
			if($tinrao){
				$i++;
			}else{
				break;
			}
			
		}
		$query = 'UPDATE tinraos SET vitrixh = '.$i.' WHERE vitrixh = '.$vitriedit;
		$this->Tinrao->query($query);
		$query = 'UPDATE tinraos SET vitrixh = '.$vitriedit.' WHERE id = '.$idtinrao;
		$this->Tinrao->query($query);
		$this->redirect(array('action' => 'admin_index'));
		
	}
	function admin_edit($id = null) {
		$this->layout = "admin";
		if(!isset($_POST['tieude'])&&!isset($_POST['noidung'])
			&&!isset($_POST['idduan'])&&!isset($_POST['tenlienhe'])
			&&!isset($_POST['vitrixh'])&&!isset($_POST['sdtlienhe'])
			&&!isset($_POST['email'])){
			//$this->data = $this->Duan->read(null, $id);
				$this->set('tinrao', $this->Tinrao->read(null, $id));
				$this->set('duans', $this->Tinrao->Duan->find('all'));
			}
		else{
			$id = $_POST['id'];
			$tieude = $_POST['tieude'];
			$noidung = $_POST['noidung'];
			$idduan = $_POST['idduan'];
			$tenlienhe = $_POST['tenlienhe'];
			$vitrixh = $_POST['vitrixh'];
			$sdtlienhe = $_POST['sdtlienhe'];
			$email = $_POST['email'];
			$ngaydang = date('Y-m-d');
			//$searchable = $this->changetv($tieude);
			$i = $vitrixh;
			while(1){
				$tinrao = $this->Tinrao->find('first', array('conditions' => array('Tinrao.vitrixh' => $i)));
				if($tinrao){
					$i++;
				}else{
					break;
				}
				
			}
			$query = 'UPDATE tinraos SET vitrixh = '.$i.' WHERE vitrixh = '.$vitrixh;
			$this->Tinrao->query($query);
			$query = 'UPDATE tinraos SET tieude = "'.$tieude.'", noidung = "'.$noidung.'", duan_id = '.$idduan.', tenlienhe = "'.$tenlienhe.'", vitrixh = '.$vitrixh.', sdtlienhe = '.$sdtlienhe.', email = "'.$email.'", ngaydang = "'.$ngaydang.'" WHERE id='.$id;
			if ($this->Tinrao->query($query)) {
				$this->Session->setFlash(__('Tin rao đã được cập nhật', true));
				$this->redirect(array('action' => 'admin_index'));
			} else {
				$this->Session->setFlash(__('Tin rao chưa được cập nhật', true));
			}
		}
	}
	function addtinrao(){
		$duans = $this->Tinrao->Duan->find('all');
		if(isset($this->params['requested'])){
            return $duans;
        }
	}
	function edit($id = null) {
		if (!$id && empty($this->data)) {
			$this->Session->setFlash(__('Invalid tinrao', true));
			$this->redirect(array('action' => 'index'));
		}
		if (!empty($this->data)) {
			if ($this->Tinrao->save($this->data)) {
				$this->Session->setFlash(__('The tinrao has been saved', true));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The tinrao could not be saved. Please, try again.', true));
			}
		}
		if (empty($this->data)) {
			$this->data = $this->Tinrao->read(null, $id);
		}
	}

	function admin_delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Không tồn tại tin rao', true));
			$this->redirect(array('action'=>'admin_index'));
		}else{
			$tinr = $this->Tinrao->find('first', array('conditions' => array('Tinrao.id' => $id)));
			$i = $tinr['Tinrao']['vitrixh'] + 1;
			if ($this->Tinrao->delete($id)) {
				while(1){
					$tinrao = $this->Tinrao->find('first', array('conditions' => array('Tinrao.vitrixh' => $i)));
					if($tinrao){
						$query = 'UPDATE tinraos SET vitrixh = '.($i-1).' WHERE vitrixh = '.$i;
						$this->Tinrao->query($query);
						$i++;
					}else{
						break;
					}
					
				}
				$this->Session->setFlash(__('Tin rao được xóa thành công', true));
				$this->redirect(array('action'=>'admin_index'));
			}
			$this->Session->setFlash(__('Tin rao chưa được xóa', true));
			$this->redirect(array('action' => 'admin_index'));
		}
	}
	function delete($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for tinrao', true));
			$this->redirect(array('action'=>'index'));
		}
		if ($this->Tinrao->delete($id)) {
			$this->Session->setFlash(__('Tinrao deleted', true));
			$this->redirect(array('action'=>'index'));
		}
		$this->Session->setFlash(__('Tinrao was not deleted', true));
		$this->redirect(array('action' => 'index'));
	}
}
?>